﻿using System;
using System.Collections.Generic;
using System.Data;
using personalplaner.business.stamm;

namespace personalplaner.dataprovider.services
{
    public partial class AbteilungDataService
    {
        internal static IEnumerable<Abteilung> m_abteilungenData(DataTable table)
        {
            bool abteilungidExists = table.Columns.Contains("abteilungid");
            bool nameExists = table.Columns.Contains("name");
            bool einsatzprotagExists = table.Columns.Contains("einsatzprotag");
            var beschreibungExists = table.Columns.Contains("beschreibung");

            foreach (DataRow row in table.Rows)
            {
                yield return new Abteilung
                {
                    ID = abteilungidExists ? row.Field<int>("abteilungid") : int.MaxValue,
                    AbteilungID = abteilungidExists ? row.Field<int>("abteilungid") : int.MaxValue,
                    Name = nameExists ? row.Field<string>("name") : string.Empty,
                    Beschreibung = beschreibungExists ? row.Field<string>("beschreibung") : string.Empty,
                    EinsaetzeProTag = einsatzprotagExists ? Convert.ToDouble(row.Field<decimal>("einsatzprotag")) : 0.00
                };
            }
        }

		private static IEnumerable<PersonAbteilung> m_personAbteilungenData(DataTable table)
		{
			var abteilungidExists = table.Columns.Contains("abteilungid");
			var nameExists = table.Columns.Contains("name");
			var einsatzprotagExists = table.Columns.Contains("einsatzprotag");
			var prsidExists = table.Columns.Contains("prsid");
			var vonExists = table.Columns.Contains("von");
			var bisExists = table.Columns.Contains("bis");
			var prozentExists = table.Columns.Contains("prozent");
			var statusExists = table.Columns.Contains("status");
			var statustextExists = table.Columns.Contains("statustext");

            foreach (DataRow row in table.Rows)
            {
                yield return new PersonAbteilung
                                {
                                    ID = abteilungidExists ? row.Field<int>("abteilungid") : int.MaxValue,
                                    AbteilungID = abteilungidExists ? row.Field<int>("abteilungid") : int.MaxValue,
                                    Name = nameExists ? row.Field<string>("name") : string.Empty,
                                    EinsaetzeProTag = einsatzprotagExists ? row.Field<int>("einsatzprotag") : 0,
                                    PrsID = prsidExists ? row.Field<int>("prsid") : 0,
                                    Von = vonExists ? row.Field<DateTime>("von") : DateTime.MinValue,
                                    Bis = bisExists ? row.Field<DateTime>("bis") : DateTime.MinValue,
                                    Prozent = prozentExists ? row.Field<int>("prozent") : 0,
                                    Status = statusExists ? row.Field<int>("status") : 0,
                                    StatusText = statustextExists ? row.Field<string>("statustext") : string.Empty
                                };
            }
		}
    }
}
